package com.newsmanagement.service.impl;

import com.newsmanagement.common.Constants;
import com.newsmanagement.common.Page;
import com.newsmanagement.dao.INewsContentServiceDao;
import com.newsmanagement.dao.ISocialDevelopmentServiceDao;
import com.newsmanagement.entity.NewsCategoryEntity;
import com.newsmanagement.entity.NewsContentEntity;
import com.newsmanagement.entity.ReturnedMessages;
import com.newsmanagement.entity.SocialDevelopmentEntity;
import com.newsmanagement.service.INewsContentService;
import com.newsmanagement.service.ISocialDevelopmentService;
import com.newsmanagement.util.CheckAndChangeUtil;
import com.newsmanagement.util.EntityMapChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xbr
 * 2017/12/4 16:29
 * 社会动态管理service实现
 */
@Service
@Transactional
public class SocialDevelopmentServiceImpl implements ISocialDevelopmentService {


    @Autowired
    private ISocialDevelopmentServiceDao socialDevelopmentServiceDao;


    /**
     * 发布动态（即保存动态，发布标记为发布）
     * @param eumId 操作的用户id
     * @param developmentTitle 动态标题
     * @param developmentTxt 动态内容
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages releaseDevelopment(Integer eumId ,String developmentTitle,String developmentTxt){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(eumId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"创建人主键未找到");
        }

        //验证动态标题
        if(!CheckAndChangeUtil.checkStringHaveData(developmentTitle)){
            developmentTitle = "";
        }

        //验证新闻内容
        if(!CheckAndChangeUtil.checkStringHaveData(developmentTxt)){
            return returnedMessages.ReturnedMessagesFail(500 ,"请编辑动态内容");
        }

        //添加新闻内容信息
        int addNumber = socialDevelopmentServiceDao.releaseDevelopment(eumId ,developmentTitle,developmentTxt ,Constants.INT_ONE);

        if (CheckAndChangeUtil.checkIntMoreThanZero(addNumber)){
            returnedMessages.setData(addNumber);
            return returnedMessages;
        }else{
            return returnedMessages.ReturnedMessagesFail(400 ,"数据插入数据库时出错");
        }
    }

    /**
     * 分页查询动态
     * @param jumpPage 跳转页面
     * @param showSize 每页显示的数据条数
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages developmentList(Integer jumpPage ,Integer showSize){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        int totalCount = socialDevelopmentServiceDao.getDevelopmentListCount();

        List<Map<String ,Object>> developmentList =  socialDevelopmentServiceDao.getDevelopmentList();

        Page page = new Page(totalCount,jumpPage,showSize);

        developmentList = developmentList.subList(page.getStartPage(),page.getEndPage());

        List list = new ArrayList();
        if (null != developmentList){
            for (int i = 0; i < developmentList.size(); i++) {
                list.add(EntityMapChange.mapToEntity(developmentList.get(i) ,SocialDevelopmentEntity.class));
            }

            page.setList(list);
            returnedMessages.setData(page);
            return returnedMessages;
        }else{
            returnedMessages.setCode(400);
            returnedMessages.setMessage("未查找到对应信息");
            return returnedMessages;
        }
    }

    /**
     * 对动态点赞
     * @param eumId 点赞用户id
     * @param developmentId 被点赞动态id
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages likeDevelopment(Integer eumId ,Integer developmentId){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(eumId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"创建人主键未找到");
        }

        //动态id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(developmentId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"动态主键未找到");
        }

        //此处原本还需要根据动态id和用户id，查询是否已经点过赞了
        List<Map<String ,Object>> list = socialDevelopmentServiceDao.queryDevelopmentByUserIdDevelopmentId(eumId ,developmentId);
        if (null != list && list.size() > Constants.INT_ZERO){
            return returnedMessages.ReturnedMessagesFail(500 ,"已经点过赞了，不可重复点赞");
        }
        list = null;

        //点赞动态内容
        int likeNumber = socialDevelopmentServiceDao.likeDevelopment(eumId ,developmentId);

        if (CheckAndChangeUtil.checkIntMoreThanZero(likeNumber)){
            returnedMessages.setData(likeNumber);
            return returnedMessages;
        }else{
            return returnedMessages.ReturnedMessagesFail(400 ,"数据插入数据库时出错");
        }
    }

}
